/***********************************************
 * Title:       CurriculumAction.java
 * Description: CurriculumAction.java
 * Author:      Niyuzhe
 * Create Date: 2010-2-5
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.education;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.infohazard.maverick.flow.ControllerContext;

import com.younker.waf.maverick.DispatchCtl;

import edu.tju.education.model.Course;
import edu.tju.education.model.CourseSchedule;
import edu.tju.education.model.Curriculum;
import edu.tju.education.model.Subject;
import edu.tju.model.TheClass;

/**
 * @author Niyuzhe
 *
 */
public class CurriculumAction extends DispatchCtl
{
        
        private final static String V_CONFIRM = "confirm";
        private final static String V_DELETE = "delete";
        private final static String V_CURRICULUM = "curriculum";
        
        Logger log = Logger.getLogger(this.getClass());
        
        private long courseId;
        private int weekDay;
        private int classNo;
        private long curriculumId;
        private long teacherId;
        private List<Curriculum> conflictTheClassCurriculum;
        private List<Curriculum> conflictTeacherCurriculum;
        private List<Curriculum> curriculums;
        private List<Speciality> specialitys;
        private List<TheClass> theClasses;
        private List<Subject> subjects;
        
        

        /* (non-Javadoc)
         * @see org.infohazard.maverick.ctl.FormBeanUser#makeFormBean(org.infohazard.maverick.flow.ControllerContext)
         */
        @Override
        protected Object makeFormBean(ControllerContext cctx)
        {
                return new CurriculumAction();
        }
        
        public String display( Object beans, ControllerContext ctx )
        {
                CurriculumAction bean = (CurriculumAction)beans;
                long courseId = bean.getCourseId();
                int weekDay = bean.getWeekDay();
                int classNo = bean.getClassNo();
                
                Curriculum curriculum = new Curriculum();
                curriculum.setCourseId(courseId);
                curriculum.setClassNo(classNo);
                curriculum.setWeekDay(weekDay);
                
                try
                {
                        List<Curriculum> conflictTheClassCurriculum = (List<Curriculum>) sqlMap.queryForList("getTheClassConflictCurriculum", curriculum);
                        bean.setConflictTheClassCurriculum(conflictTheClassCurriculum);
                        List<Curriculum> conflictTeacherCurriculum = (List<Curriculum>) sqlMap.queryForList("getTeacherConflictCurriculum", curriculum);
                        bean.setConflictTeacherCurriculum(conflictTeacherCurriculum);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                
                return V_CONFIRM;
        }
        
        public String confirm( Object beans, ControllerContext ctx)
        {
                CurriculumAction bean = (CurriculumAction)beans;
                long courseId = bean.getCourseId();
                int weekDay = bean.getWeekDay();
                int classNo = bean.getClassNo();
                
                Curriculum curriculum = new Curriculum();
                curriculum.setCourseId(courseId);
                curriculum.setClassNo(classNo);
                curriculum.setWeekDay(weekDay);
                
                try
                {
                        sqlMap.insert("insertCurriculum", curriculum);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return V_CONFIRM;
        }
        
        public String delete( Object beans, ControllerContext ctx )
        {
                
                long curriculumId = ((CurriculumAction)beans).getCurriculumId();
                log.debug(curriculumId);
                try
                {
                        sqlMap.delete("deleteCurriculum", curriculumId);
                } catch (SQLException e)
                {
                        log.error("delete curriculum error[curriculumId=" + curriculumId + "].", e);
                        ctx.getRequest().setAttribute(RET_CODE, RC_FAILURE);
                        return V_DELETE;
                }
                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                return V_DELETE;
        }
        
        public String showCurriculum(Object beans, ControllerContext ctx)
        {
                long teacherId = ((CurriculumAction)beans).getTeacherId();
                try
                {
                        List<Curriculum> curriculums = sqlMap.queryForList("getCurriculumThisTermByTeacherId", teacherId);
                        List<Course> courses = new ArrayList<Course>(curriculums.size());
                        log.debug("courses size=" + courses.size());
                        log.debug("size=" + curriculums.size());
                        for( int i = 0; i<curriculums.size(); i++ )
                        {
                                courses.add(i, 
                                                (Course)sqlMap.queryForObject("getCourseById", curriculums.get(i).getCourseId() ) );
                        }
                        List<TheClass> theClasses = new ArrayList<TheClass>(curriculums.size());
                        for( int i = 0; i<courses.size(); i++ )
                        {
                                theClasses.add(i, (TheClass)sqlMap.queryForObject("getTheClassById", courses.get(i).getTheClassId()));
                        }
                        List<Speciality> specialitys = new ArrayList<Speciality>(theClasses.size());
                        for( int i = 0; i<theClasses.size(); i++ )
                        {
                                specialitys.add(i, (Speciality)sqlMap.queryForObject("getSpecialityById", theClasses.get(i).getSpecialityId()));
                                log.debug(specialitys.get(i));
                        }
                        List<CourseSchedule> courseSchedules = new ArrayList<CourseSchedule>(courses.size());
                        for( int i = 0; i<courses.size(); i++ )
                        {
                                courseSchedules.add(i, (CourseSchedule)sqlMap.queryForObject("getCourseScheduleById", courses.get(i).getCourseScheduleId()));
                        }
                        List<Subject> subjects = new ArrayList<Subject>(courseSchedules.size());
                        for( int i = 0; i<courseSchedules.size(); i++)
                        {
                                subjects.add(i, (Subject)sqlMap.queryForObject("getSubjectById", courseSchedules.get(i).getSubjectId()));
                        }
                        
                        CurriculumAction bean = (CurriculumAction) beans;
                        bean.setCurriculums(curriculums);
                        bean.setTheClasses(theClasses);
                        bean.setSpecialitys(specialitys);
                        bean.setSubjects(subjects);
                        
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return V_CURRICULUM;
        }

        /**
         * @return the courseId
         */
        public long getCourseId()
        {
                return courseId;
        }

        /**
         * @param courseId the courseId to set
         */
        public void setCourseId(long courseId)
        {
                this.courseId = courseId;
        }

        /**
         * @return the weekDay
         */
        public int getWeekDay()
        {
                return weekDay;
        }

        /**
         * @param weekDay the weekDay to set
         */
        public void setWeekDay(int weekDay)
        {
                this.weekDay = weekDay;
        }

        /**
         * @return the classNo
         */
        public int getClassNo()
        {
                return classNo;
        }

        /**
         * @param classNo the classNo to set
         */
        public void setClassNo(int classNo)
        {
                this.classNo = classNo;
        }

        /**
         * @return the conflictTheClassCurriculum
         */
        public List<Curriculum> getConflictTheClassCurriculum()
        {
                return conflictTheClassCurriculum;
        }

        /**
         * @param conflictTheClassCurriculum the conflictTheClassCurriculum to set
         */
        public void setConflictTheClassCurriculum(List<Curriculum> conflictTheClassCurriculum)
        {
                this.conflictTheClassCurriculum = conflictTheClassCurriculum;
        }

        /**
         * @return the conflictTeacherCurriculum
         */
        public List<Curriculum> getConflictTeacherCurriculum()
        {
                return conflictTeacherCurriculum;
        }

        /**
         * @param conflictTeacherCurriculum the conflictTeacherCurriculum to set
         */
        public void setConflictTeacherCurriculum(List<Curriculum> conflictTeacherCurriculum)
        {
                this.conflictTeacherCurriculum = conflictTeacherCurriculum;
        }

        /**
         * @return the curriculumId
         */
        public long getCurriculumId()
        {
                return curriculumId;
        }

        /**
         * @param curriculumId the curriculumId to set
         */
        public void setCurriculumId(long curriculumId)
        {
                this.curriculumId = curriculumId;
        }

        /**
         * @param teacherId the teacherId to set
         */
        public void setTeacherId(long teacherId)
        {
                this.teacherId = teacherId;
        }

        /**
         * @return the teacherId
         */
        public long getTeacherId()
        {
                return teacherId;
        }

        /**
         * @return the curriculums
         */
        public List<Curriculum> getCurriculums()
        {
                return curriculums;
        }

        /**
         * @param curriculums the curriculums to set
         */
        public void setCurriculums(List<Curriculum> curriculums)
        {
                this.curriculums = curriculums;
        }

        /**
         * @return the specialitys
         */
        public List<Speciality> getSpecialitys()
        {
                return specialitys;
        }

        /**
         * @param specialitys the specialitys to set
         */
        public void setSpecialitys(List<Speciality> specialitys)
        {
                this.specialitys = specialitys;
        }

        /**
         * @return the theClasses
         */
        public List<TheClass> getTheClasses()
        {
                return theClasses;
        }

        /**
         * @param theClasses the theClasses to set
         */
        public void setTheClasses(List<TheClass> theClasses)
        {
                this.theClasses = theClasses;
        }

        /**
         * @return the subjects
         */
        public List<Subject> getSubjects()
        {
                return subjects;
        }

        /**
         * @param subjects the subjects to set
         */
        public void setSubjects(List<Subject> subjects)
        {
                this.subjects = subjects;
        }

}
